package beanstao.util.store.presenter;

import org.dom4j.Element;

import beanstao.util.collections.Pair;
import beanstao.util.id.DId;
import beanstao.util.id.Id;
import beanstao.util.response.PD;
import beanstao.util.response.PDs;
import beanstao.util.store.IQD;
import beanstao.util.store.KV;
import beanstao.util.store.dal.XmlTreeDal;
import beanstao.util.store.domainobject.DomainList;
import beanstao.util.store.domainobject.XmlDomain;
import beanstao.util.store.domainobject.fields.PublicField;
import beanstao.util.xml.XDocument;


public abstract class AbstractXMLTreePresenter extends AbstractXMLPresenter implements ITreePresenter
{

	@Override
	public abstract XmlTreeDal<XmlDomain> getDal();

	@Override
	public PD ancestor(final String id)
	{
		return PDs.builder(this.getDal().selectAncestor(DId.ME(id)));
	}

	@Override
	public PD create(final IQD data)
	{
		XmlDomain branch = this.getDal().newBlankDomain();
		branch.fromRequest(data);
		Object obj = data.get("parentid").$V();
		if (obj == null)
			return null;
		else
			return PDs.builder(this.getDal().create(DId.ME(data.get(PublicField.PARENT_ID).$V().toString()), branch));
	}

	@Override
	public PD childs(IQD data)
	{
		Id id = null;
		if ((null != data))
			id = DId.ME(data.get(PublicField.ID).$V().toString());
		DomainList<XmlDomain> list = this.getDal().selectChilds(id);
		return PDs.builder(list);
	}

	@Override
	public PD elements(IQD data)
	{
		Id id = null;
		if ((null != data))
			id = DId.ME(data.get(PublicField.PARENT_ID).$V().toString());
		return PDs.builder(this.getDal().selectElements(id));
	}

	@Override
	public PD idNameOfChilds(IQD data)
	{
		Id id = DId.ME(data.get("id").$V().toString());
		return PDs.builder(this.getDal().childAttributes(id, "name"));
	}

	@Override
	public PD idNameOfElements(IQD data)
	{
		Id id = DId.ME(data.get("id").$V().toString());
		return PDs.builder(this.getDal().elementAttributes(id, "name"));
	}

	@Override
	public PD idOfChilds(IQD data)
	{
		Id id = DId.ME(data.get("id").$V().toString());
		return PDs.builder(this.getDal().childAttributes(id, "name"));
	}

	@Override
	public PD idOfElements(IQD data)
	{
		Id id = DId.ME(data.get("id").$V().toString());
		return PDs.builder(this.getDal().elementAttributes(id, "id"));
	}

	@Override
	public PD simpleXml(IQD data)
	{
		KV idKValue = data.get(PublicField.ID);
		if (null == idKValue)
			return PDs.builder("IDisNull");
		Id id = DId.ME(idKValue.$V().toString());
		Element all = this.getDal().xml(id);
		@SuppressWarnings("unchecked")
		Element result = XDocument.simpleXml(all, Pair.ME("isDeleted", "1"));
		return PDs.builder(result);
	}

	@Override
	public PD xml(IQD data)
	{
		Id id = DId.ME(data.get(PublicField.ID).$V().toString());
		return PDs.builder(this.getDal().xml(id));
	}

	@Override
	public PD parent(IQD data)
	{
		Id id = null;
		if ((null != data))
			id = DId.ME(data.get(PublicField.ID).$V().toString());
		return PDs.builder(this.getDal().selectParent(id));
	}

	@Override
	public Boolean existElement(Id parentId, Id elementId)
	{
		return this.getDal().existElement(parentId, elementId);
	}

	@Override
	public Boolean existElement(Id parentId, String attrubuteName, String attrubuteValue)
	{
		return this.getDal().existElement(parentId, attrubuteName, attrubuteValue);
	}
}
